//
//  MainViewController.m
//  Funny
//
//  Created by dllo on 15/10/27.
//  Copyright © 2015年 King. All rights reserved.
//

#import "MainViewController.h"
#import "MainTableViewCell.h"
#import "MyModel.h"

@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView; 
@property (nonatomic, strong) NSMutableArray *dataArr; // 存放数据数组

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self handleData];
    self.title = @"推荐";
    [self.view addSubview:self.tableView];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"search@3x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick:)];

    UIButton *toTop = [UIButton buttonWithFrame:CGRectMake(ScreenWidth - 50, ScreenHeight - 170, 40, 40) color:[UIColor blackColor] title:nil type:UIButtonTypeSystem target:self action:@selector(backToTopWithBtn:) event:UIControlEventTouchUpInside cornerRadius:20];
    toTop.alpha = 0.5;
    [toTop setImage:[UIImage imageNamed:@"backTop@3x.png"] forState:UIControlStateNormal];
    toTop.tintColor = [UIColor colorWithRed:220.0 / 255.0 green:220.0 / 255.0 blue:220.0 / 255.0 alpha:1.0];
    [self.view addSubview:toTop];
    
    // Do any additional setup after loading the view.
}

- (void)backToTopWithBtn:(UIButton *)btn
{
    if (self.tableView.contentOffset.y == 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经到顶" preferredStyle:1];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:okAction];
        [self presentViewController:alert animated:NO completion:^{
            
        }];
    }
    else{
        self.tableView.contentOffset = CGPointMake(0, 0);
    }
}

- (void)buttonClick:(UIButton *)btn
{
    SearchViewController *searchVC = [SearchViewController shareViewController];
    [self.navigationController pushViewController:searchVC animated:NO];
}

// 视图出现时 tabbar显示
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

// statusBar 白色
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

// 存放数据数组GET方法
- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        self.dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

// tableView GET方法
- (UITableView *)tableView
{
    if (!_tableView) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 66) style:UITableViewStylePlain];
        self.tableView.showsVerticalScrollIndicator = NO;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.tableView registerClass:[MainTableViewCell class] forCellReuseIdentifier:@"reuse"];
    }
    return _tableView;
}

// 数据处理
- (void)handleData
{
    [NetHandler getDataWithURL:@"http://api.mxtrip.cn/api/area/getCityList?cid=kWXF&_tk=1446538647:10788&apiVersion=1.1&clientVersion=1.2.0&idfa=A857AE53-A1D1-4331-9E4E-62937AB3C688" completion:^(NSData *data) {
        if (data != nil) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            for (NSDictionary *dic1 in [dic objectForKey:@"data"]) {
                MainModel *model = [MainModel modelWithDic:dic1];
                [self.dataArr addObject:model];// 封装数据基类方法
            }
            [_tableView reloadData];
        }
    }];
}

#pragma mark - tableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CityDetailViewController *cityVC = [CityDetailViewController shareViewController];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    cityVC.ID = [_dataArr[indexPath.row] ID];
    [self.navigationController pushViewController:cityVC animated:NO];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}

// 视差视图的效果
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    for (MainTableViewCell *cell in self.tableView.visibleCells) {
        cell.scroll.contentOffset = CGPointMake(0, (cell.frame.origin.y - scrollView.contentOffset.y) * 0.5);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
